//
//  YWRemindListTableViewController.m
//  maintain
//
//  Created by kyo on 17/2/7.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWRemindListTableViewController.h"
#import "UIColor+CustomColors.h"
#import "MJRefresh.h"
#import "YWConst.h"
#import "YWMacro.h"
#import "YWLoginTool.h"

#import "YWRemindListCell.h"
#import "YWWorkReminderViewController.h"

#import "YWReminder.h"
#import "YWWorkReminderManager.h"
#import "YWMissionManager.h"
#import "YWKeepWatchMissionManager.h"
#import "YWInspectionMissionManager.h"
#import "YWMeterReadingManager.h"
#import "YWAttendanceManager.h"
#import "YWReplyManager.h"
#import "YWDailyReportTVC.h"

#import "YWNoReadReplyTableViewController.h"

@interface YWRemindListTableViewController ()
@property (nonatomic, strong) NSMutableArray * dataSource;
@property (nonatomic, strong) NSMutableArray *unReadReplyArray;
@end

@implementation YWRemindListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"workRemind", nil);//@"工作提醒";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshRemindOfUploadFailedCount) name:KPushBackgroundUploadRecordSuccessNotification object:nil];
    
    [self setupTableView];
}

- (void)setupTableView{
    [self.tableView registerClass:[YWRemindListCell class] forCellReuseIdentifier:YWRemindListCellIdentifier];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTop)];
    self.tableView.mj_footer.automaticallyHidden = YES;
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
};

- (NSMutableArray*)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
        //是否有离线的任务
        BOOL isExist = [[YWWorkReminderManager shareManager] existUploadFailedMissions];
        if (isExist) {
            YWReminder *reminder = [[YWReminder alloc] init];
            reminder.type = YWReminderType_uploadFailed;
            reminder.unreadCount = 1;
            reminder.createTime = [[NSDate date] timeIntervalSince1970] * 1000;
            [_dataSource addObject:reminder];
        }
        //构建工作日报
        YWReminder *reminder = [[YWReminder alloc] init];
        reminder.type = YWReminderType_dailyRport;
        reminder.unreadCount = 1;
        reminder.createTime = [[NSDate date] timeIntervalSince1970] * 1000;
        [_dataSource addObject:reminder];
        //是否有未读评论消息
        NSArray *unReadReplyArray = self.unReadReplyArray;
        if (unReadReplyArray.count) {
            YWReminder *reminder = [[YWReminder alloc] init];
            reminder.type = YWReminderType_replyMessage;
            reminder.unreadCount = unReadReplyArray.count;
            reminder.createTime = [[NSDate date] timeIntervalSince1970] * 1000;
            [_dataSource addObject:reminder];
        }
        NSArray *reminderArray = [[YWWorkReminderManager shareManager] reminderListDateArray];
        [_dataSource addObjectsFromArray:reminderArray];
    }
    return _dataSource;
}

- (void)refreshTop{
    //请求提醒列表数据
    YWWeakSelf(weakSelf);
    [[YWWorkReminderManager shareManager] requestReminderListWithComplete:^(BOOL succeed) {
        [weakSelf.tableView.mj_header endRefreshing];
        if (succeed) {
            NSInteger totalCount = [[YWWorkReminderManager shareManager] unreadRemindTotalCount];
            [YWLoginTool saveWorkReminderNoRedWithCount:totalCount];
            
            weakSelf.dataSource = nil;
            [weakSelf.tableView reloadData];
        }
    }];
    [[YWWorkReminderManager shareManager] requestDailyReportWithAnalysisDate:nil complete:^(BOOL succeed) {
        
    }];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YWRemindListCell * cell = [tableView dequeueReusableCellWithIdentifier:YWRemindListCellIdentifier];
    cell.reminder = self.dataSource[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 62;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YWReminder *reminder = self.dataSource[indexPath.row];
    if (reminder.type != YWReminderType_replyMessage && reminder.type != YWReminderType_uploadFailed) {
        [[YWWorkReminderManager shareManager] updateReminderLastTotalCountWithType:reminder.type];
        
        NSInteger totalCount = [[YWWorkReminderManager shareManager] unreadRemindTotalCount];
        [YWLoginTool saveWorkReminderNoRedWithCount:totalCount];
    }
    if (reminder.type != YWReminderType_uploadFailed) {
        reminder.unreadCount = 0;
    }
    //[self.tableView reloadData];
    
    if (reminder.type == YWReminderType_replyMessage) {
        YWNoReadReplyTableViewController *messageTableVC = [[YWNoReadReplyTableViewController alloc]init];
        
        messageTableVC.replysArray = [[NSArray alloc] initWithArray:self.unReadReplyArray];
        [self.navigationController pushViewController:messageTableVC animated:YES];
        [[YWReplyManager shareManager] updateReplyIsReadWithType:YWReplyRelationTypeAll];
        [_dataSource removeObject:reminder];
        //[self.tableView reloadData];
//        if (self.clearUnReadReplysCountBlock) {
//            self.clearUnReadReplysCountBlock();
//        }
    }else if (reminder.type == YWReminderType_dailyRport){
        YWDailyReportTVC * dailyReportVC = [YWDailyReportTVC new];
        [self.navigationController pushViewController:dailyReportVC animated:YES];
    }else{
        YWWorkReminderViewController *workReminderVC = [[YWWorkReminderViewController alloc] init];
        workReminderVC.reminderType = reminder.type;
        [self.navigationController pushViewController:workReminderVC animated:YES];
    }
    
    [self.tableView reloadData];
}

- (NSMutableArray *)unReadReplyArray{
    if (!_unReadReplyArray) {
        _unReadReplyArray = [NSMutableArray array];
    }
    [_unReadReplyArray removeAllObjects];
    NSArray *noReadRecords = [[YWReplyManager shareManager] noReadReplysWithRelationType:YWReplyRelationTypeAll];
    [_unReadReplyArray addObjectsFromArray:noReadRecords];
    return _unReadReplyArray;
}

- (void)refreshRemindOfUploadFailedCount{
    _dataSource = nil;
    [self.tableView reloadData];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
